/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-03-23 15:57:09
 * @LastEditors: TT
 * @LastEditTime: 2023-03-13 11:16:31
 */

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ttcomment/ttcomment.dart';
import 'hzy_normal_getx_list_controller.dart';
import 'hzy_normal_utils.dart';

abstract class NormaGetxView<T extends NormalGetxController> extends GetView<T>
    with AbstractAttribute, AbstractWidget {
  NormaGetxView({Key? key}) : super(key: key);
  final bool isAllObx = true;
  @override
  Widget build(BuildContext context) {
    Widget widget = Container();
    if (isneedScaffol) {
      widget = createScaffol(
        context: context,
        safeAreatop: safeAreatop,
        safeAreabottm: safeAreabottm,
        pageState: pageState,
        resizeToAvoidBottomInset: resizeToAvoidBottomInset,
        title: title,
        backgroundColor: backgroundColor,
        navbackcolor: navbackcolor,
      );
    } else {
      widget = isAllObx
          ? Obx(
              () => createBody(
                context,
              ),
            )
          : createBody(
              context,
            );
    }
    return widget;
  }

  @override
  Widget createCommBaseWidget(BuildContext context, PageState pageState) {
    return createColumWidget(context);
  }

  /// 抽离通用colum 方法,用layoutbuilder包裹的目的
  /// 为了解决web 开发时,设置最大布局
  /// 分为 createObxWidget() 和 createObxColumWidget()
  @override
  Widget createColumWidget(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: LayoutBuilder(
        builder: (context, constraints) {
          configlayoutbuiderConstraints(constraints);
          return Container(
            alignment: Alignment.centerLeft,
            decoration: createBoxDecoration(),
            width: configSizeBoxWidth(constraints),
            child: createObxWidget(),
          );
        },
      ),
    );
  }

  /// 为 getx 新增包裹方法
  Widget createObxWidget() {
    return isAllObx
        ? Obx(
            () => CommonBasePage(
              isshowloading: controller.isshowloading.value,
              pageState: controller.pageState.value,
              child: createObxColumWidget(),
              loadingWidget: createLoadingWidget(),
              errorWidget: createErrorWidget(),
            ),
          )
        : CommonBasePage(
            isshowloading: controller.isshowloading.value,
            pageState: controller.pageState.value,
            child: createObxColumWidget(),
            loadingWidget: createLoadingWidget(),
            errorWidget: createErrorWidget(),
          );
  }

  /// 配置单个Obx Widget
  Widget configNormalObxWiget({
    required Widget Function() builder,
  }) {
    return Obx(builder);
  }

  /// getx 真实包裹的colum 方法
  Widget createObxColumWidget() {
    return Column(
      children: [
        createHeaderWidget(),
        Expanded(
          child: createBody(
            Get.context!,
          ),
        ),
      ],
    );
  }

  createBoxDecoration() {
    return null;
  }

  /// 获取 屏幕 最大尺寸
  configlayoutbuiderConstraints(BoxConstraints constraints) {
    controller.configsiezw(configSizeBoxWidth(constraints));
  }

  double configSizeBoxWidth(BoxConstraints constraints) {
    return HzyNormalUtils.configSizeMaxW(constraints.maxWidth);
  }
}
